﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 循环地图
public class LoopMap
{
    List<MapBlock> list = new List<MapBlock>();

    public float CachedMapLength {  get; private set; }

    public float NextPos { get; private set; }

    int _maxCache;

    public void Init(int maxCache)
    {
        _maxCache = maxCache;
    }

    public GameObject Add(GameObject go, float length)
    {
        var block = new MapBlock() { Go = go, Length = length };

        list.Add(block);
        CachedMapLength += block.Length;
        NextPos += block.Length; // 只增不减

        if (list.Count > _maxCache)
        {
            var first = list[0];
            list.RemoveAt(0);
            CachedMapLength -= first.Length;

            GameObject.Destroy(first.Go);
        }

        return go;
    }

    public GameObject GetLast()
    {
        if (list.Count <= 0) { return null; }

        return list.Last().Go;
    }

    // 地图块
    private class MapBlock
    {
        public GameObject Go;
        public float Length;
    }
}
